package com.study.leetcode;

import java.util.ArrayList;
import java.util.List;

//https://leetcode-cn.com/problems/permutations/comments/
public class Solution_46 {
    List<Integer> cur = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        permute(nums, result);
        return result;
    }

    public void permute(int[] nums, List<List<Integer>> result) {
        if (cur.size() == nums.length) {
            result.add(new ArrayList<>(cur));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == Integer.MAX_VALUE) continue;
            Integer temp = nums[i];
            cur.add(temp);
            nums[i] = Integer.MAX_VALUE;
            permute(nums, result);
            cur.remove(temp);
            nums[i] = temp;
        }
    }
}
